import pamie
import time
import  win32com

f= open('a.txt','w')
def getLink(pLink):
	print 'sub main link------------> ' + pLink + "\n"
	ie.Navigate(pLink)
	while (ie.ReadyState != 4):
		time.sleep(0.05)

	hrefs = ie.Document.getElementsByTagName("a")
	
	ie.Quit
	#print hrefs.length
	f.write("\n<---------------------------------------------------"+pLink+"-------------------------------------------------------------->\n")
	for n1 in range(1,int(hrefs.length)):
		#if n1 != 4 :
		#	print hrefs[n1]
		#line = unicodedata.normalize('NFKD', unicode(hrefs[n1])).encode('ascii','ignore')
		f.write(str(hrefs[n1]))
		f.write("\n")
	f.write("\n<----------------------------------------------------parent link finished--------------------------------------------------------------->\n")
ie=win32com.client.Dispatch("InternetExplorer.Application")
#ie.Navigate('file:///C:/Documents%20and%20Settings/Administrator/code/ascii/t.htm')
ie.Navigate('http://search.yahoo.com/search;_ylt=A0oGdV8K6tNQ3A4A0UlXNyoA?p=last.fm&fr2=sb-top&fr=yfp-t-701')
ie.Visible = 0
while (ie.ReadyState != 4):
    time.sleep(0.05)

hrs = ie.Document.getElementsByTagName("a")

for n in range(0,int(hrs.length)):
	#getLink(str(hrs[n]))
	hr = str(hrs[n])
	#f.write(hr)
	#f.write("\n")
	if hr.find("search") > 0 and len(hr) < 133: 
		getLink(hr)
time.sleep(3)
ie.Quit()
f.close()